<?php
/**
 * @Created on 2020/01/15
 * @Author QQ 1026269609@qq.com
*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Member extends Base
{
    public function index(){
		$data=Db::table('dsp_member a,dsp_auth_group b')->field('a.uid,a.username,b.title,a.bumenid,a.partid')->where('a.uid !=1')->where('a.roleid=b.id')->order('a.uid desc')->paginate(10);//不显示管理员
		$this->assign('data',$data);
        return $this->fetch();
    }
    public function addmember(){
    	if(request()->isPost()){
			$roleid=request()->param('roleid');
			$username=request()->param('username');
			$password=request()->param('password');
			$bumenid=request()->param('bumenid');
			$partid=request()->param('partid');
			$data=['roleid'=>$roleid,'username'=>$username,'password'=>md5($password),'bumenid'=>$bumenid,'partid'=>$partid];
			$info=db('member')->where('username',$username)->find();
			if($info){
				return json(['status'=>0,'message'=>'用户名已存在']);
			}
			$true=db('member')->insertGetId($data);//新增数据并返回主键值
			if($true){
				db('auth_group_access')->insert(['uid'=>$true,'group_id'=>$roleid]);//添加角色用户对应信息
				return json(['status'=>1,'message'=>'添加成功']);
			}else{
				return json(['status'=>0,'message'=>'添加失败']);
			}
    	}else{
    		$role=db('auth_group')->where('status','1')->order('id desc')->select();
    		$this->assign('role',$role);
    		$bumen=db('bumen')->order('id desc')->select();
    		$this->assign('bumen',$bumen);
			return $this->fetch();
    	}
    }
    public function delmember(){
    	$uid=request()->param('uid');
		$true=db('member')->where('uid',$uid)->delete();
		if($true){
			db('auth_group_access')->where('uid',$uid)->delete();//删除角色用户对应信息
			return json(['status'=>1,'message'=>'删除成功']);
		}else{
			return json(['status'=>0,'message'=>'删除失败']);
		}
    }
   	public function password(){
   		if(request()->isPost()){
   			$uid=request()->param('uid');
			$password=request()->param('password');
			$info=db('member')->where(['uid'=>$uid,'password'=>md5($password)])->find();
			if($info){
				return json(['status'=>0,'message'=>'新密码与旧密码相同，请重新修改']);
			}
			db('member')->where('uid',$uid)->update(['password'=>md5($password)]);
			return json(['status'=>1,'message'=>'修改成功']);
		}else{
			$uid=request()->param('uid');
			$info=db('member')->where('uid',$uid)->find();
			$this->assign('info',$info);
			return $this->fetch();
		}
   	}
}